"use strict";
class LinkedListStack {
    constructor() {
        this.stkSize = 0;
        this.stackPeek = null;
    }
    // 获取栈长度
    get size() {
        return this.stkSize;
    }
    isEmpty() {
        return this.stkSize === 0;
    }
    // 入栈
    push(num) {
        const node = new ListNode(num);
        node.next = this.stackPeek;
        this.stackPeek = node;
        this.stkSize++;
    }
    // 出栈
    pop() {
        const num = this.peek();
        if (!this.stackPeek) {
            throw new Error('栈为空');
        }
        this.stackPeek = this.stackPeek.next;
        this.stkSize--;
        return num;
    }
    // 访问栈顶元素
    peek() {
        if (!this.stackPeek)
            throw new Error('栈为空');
        return this.stackPeek.val;
    }
    // 将栈转为数组并输出
    toArray() {
        if (!this.stackPeek) {
            throw new Error('栈为空');
        }
        let node = this.stackPeek;
        const res = new Array(this.size);
        for (let i = res.length - 1; i >= 0; i--) {
            res[i] = node.val;
            node = node.next;
        }
        return res;
    }
}
